package com.mall.shopping.services;

import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Hu
 * @create 2021-09-29 9:53
 * @Description
 */
@Slf4j
@Service
public class ProductCateServiceImpl implements IProductCateService {

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;


    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {

        AllProductCateResponse response = new AllProductCateResponse();


        try {
            //参数校验
            request.requestCheck();
            //调用mapper查询相应cateinfo  给 example填入条件

            Example example = new Example(ItemCat.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("status", 1);
            List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
            List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);

            response.setProductCateDtoList(productCateDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            log.info("com.mall.shopping.services.ProductCateServiceImpl.getAllProductCate success query rows"+productCateDtos.size());

        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            log.info("com.mall.shopping.services.ProductCateServiceImpl.getAllProductCate success query rows"+e);

        }

        return response;
    }
}
